package cn.edu.jxust.core.infrastructure.mapper;

import cn.edu.jxust.common.domain.entity.Aqi;
import cn.edu.jxust.common.domain.valobj.AirVo;
import cn.edu.jxust.common.domain.dto.MainPollutionDayVo;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;

/**
 * @author QiuKangming
 * @since 2020-06-29
 */

public interface AqiMapper extends BaseMapper<Aqi> {

    /**
     * 查询污染物分担率, 就是每种气体的在某个时间范围内的平均值
     * @param deviceId 设备id
     * @param aqiType aqi类型
     * @param begin 开始时间
     * @param end 结束时间
     * @return List<AirVo>
     */
    @Select("SELECT DATE_FORMAT(create_datetime,'%Y-%m-%d') AS date, air_name, AVG(avg_airvalue) concentration FROM t_aqi JOIN" +
            " t_airtype WHERE t_airtype.id = t_aqi.airtype_id AND airtype_id IS NOT NULL AND device_id = #{deviceId} " +
            "AND aqi_type = #{aqiType} AND create_datetime BETWEEN #{begin} AND #{end} AND t_aqi.status = 1" +
            " GROUP BY create_datetime,air_name")
    List<AirVo> getPollutionShareRate(Integer deviceId, String aqiType, String begin, String end);


    /**
     * 查询污染物在某段时间范围内首要污染物的天数集合, 计算每种污染物所占的天数
     * @param deviceId 设备id
     * @param aqiType aqi类型
     * @param begin 开始时间
     * @param end 结束时间
     * @return List<AirVo>
     */
    @Select("SELECT main_pollution, count(main_pollution) AS total_days FROM t_aqi WHERE airtype_id IS NULL " +
            "AND device_id = #{deviceId} AND aqi_type = #{aqiType} AND create_datetime BETWEEN #{begin} AND #{end} " +
            "AND status = 1 GROUP BY main_pollution")
    List<MainPollutionDayVo> getMainPollutionDayVoList(Integer deviceId, String aqiType, String begin, String end);

    /**
     * 获取 t_aqi 表中前一天设备上传数据的设备ids
     * @param begin 开始日期时间
     * @param end 结束日期时间
     * @return List<Integer>
     */
    @Select("SELECT DISTINCT device_id FROM t_aqi WHERE airtype_id IS NOT NULL AND create_datetime BETWEEN" +
            " #{begin} AND #{end} AND status = 1")
    List<Integer> getLastDayDeviceIds(String begin, String end);

}
